﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VideoRentalShop.Model;
using System.Data.Entity;

namespace VideoRentalShop.Controller.Data
{
    public abstract class DataSourceBase<T>
    {
        internal ErrorReceiver Error { get; set; }
        protected MovieDbContext _db;


        public DataSourceBase(MovieDbContext db)
        {
            Error = ErrorReceiver.Instance;
            _db = db;
        }


        public abstract bool IsValid(T item);
        public abstract bool Add(T item);
        public abstract bool Edit(T item);
        public abstract bool Remove(T item);


        protected bool Save()
        {
            try
            {
                _db.SaveChanges();
            }
            catch (Exception e)
            {
                if (Error != null)
                {
                    Error.Catched(e);
                }
                return false;
            }

            return true;
        }
    }
}
